<?php

namespace App\Http\Controllers\Manage;


use App\Manage\OrderManage;
use App\Models\Event;
use App\Models\Order;
use App\Models\User;

class OrderUserController extends BaseController
{

    // 用户
    public function initialize()
    {
        parent::initialize();

        $this->model = 'OrderUser';

        $opt = $this->optLogic();
        $_id = $opt->getData('order_id', 0, 'intval');
        $opt->assign('init_url', Url('/manage/order-user/lists?is_pop=1&order_id='.$_id));
    }


    // 列表
    // 添加
    // 修改
    // 支付
    // 删除

    // 列表数据展示
    protected function listTable($lists)
    {
        // 整理
        $trList = [];
        if(empty($lists)){
            return $trList;
        }


        // 获取订单信息
        $oids   = array_column($lists, 'order_id');
        $orders = (new Order())->isCache(true)->getListInIds($oids);
        // 获取用户信息
        $uids  = array_column($lists, 'uid');
        $users = (new User())->isCache(true)->getListInIds($uids);
        // 活动信息
        $eids   = array_column($lists, 'event_id');
        $events = (new Event())->isCache(true)->getListInIds($eids);

        // 展示列表表格
        $listHtml = $this->adminList();
        $sexes    = [1 => '男', 2 => '女', 0 => '保密'];
        foreach($lists as $key => $item){
            // 数据
            $_id = $item['id'];
            //$status   = $item['status'] == 1 ? '正常' : '禁用';
            //$statusOp = $item['status'] == 1 ? '禁用' : '启用';
            // 操作
            $operate = '';
            //$operate .= $listHtml->operate_status(['id' => $_id, 'value' => 1 - $item['status']], $statusOp, $item['status']);
            //$operate .= $listHtml->operate_edit(['id' => $_id], '编辑');
            //$operate .= $listHtml->operate_soft_delete(['id' => $_id], '你确定要删除该订单吗？');

            // 页面
            $tr = $listHtml->list_check($item['id']);
            $tr .= $listHtml->list_td($item['id']);
            $tr .= $listHtml->list_td($users[$item['uid']]['nickname'] ?? '-');
            $tr .= $listHtml->list_td($orders[$item['order_id']]['order_no'] ?? '-');
            $tr .= $listHtml->list_td($events[$item['event_id']]['event_name'] ?? '-');
            $tr .= $listHtml->list_td($item['person_name']);
            $tr .= $listHtml->list_image($item['head_img']);
            $tr .= $listHtml->list_td($sexes[$item['sex']] ?? '-');
            $tr .= $listHtml->list_td($item['age']);
            $tr .= $listHtml->list_td($item['guardor']);
            $tr .= $listHtml->list_td($item['mobile']);
            //$tr .= $listHtml->list_td($item['status']);
            $tr .= $listHtml->list_td(date('Y-m-d H:i', $item['ctime']));
            //$tr .= $listHtml->list_td($operate);

            // 整合
            $trList[] = $listHtml->list_tr($tr);
        }

        // 整合
        return $trList;
    }

    // 列表顶部按钮
    protected function listTopButton()
    {
        $o_id   = $this->optLogic()->getData('order_id', 0, 'intval');
        $addUrl = Url('/manage/order-user/add?order_id='.$o_id);
        $delUrl = Url('/manage/order-user/batch-delete?order_id='.$o_id);

        $top_button = '<a href="javascript:;" onclick="onEdit(\''.$addUrl.'\')" class="btn btn-success radius">添加订单用户</a> ';
        //$this->assign('top_button', $top_button);

        $this->assign('top_button', '');
    }

    // 列表字段
    protected function list_fields()
    {
        // 'status' => '状态', 'operate' => '操作',
        return ['id' => 'ID', 'uid' => '用户ID', 'order_id' => '订单', 'event_id' => '活动ID', 'person_name' => '姓名', 'head_img' => '头像', 'sex' => '性别', 'age' => '年龄', 'guardor' => '监护人', 'mobile' => '手机号', 'ctime' => '创建时间',];
    }

    // 列表搜索
    protected function list_search()
    {
        $model   = new Order();
        $request = $this->optLogic()->getData();
        $form    = $this->adminList();
        $uid     = $form->formInput('uid', $request['uid'] ?? null, '用户id', '120px');

        $order_no = $form->formInput('uid', $request['order_no'] ?? null, '订单编号', '180px');
        $event_id = $form->formInput('event_id', $request['event_id'] ?? null, '活动id', '120px');
        $search   = ['uid' => $uid, 'event_id' => $event_id, 'order_no' => $order_no,];
        //$this->assign('search', $search);
        $this->assign('search', []);

        $searchHide = ['order_id'];
        $this->assign('searchHide', ['order_id']);
    }

    // 表单页面
    protected function formTable()
    {
        $manage = $this->checkManage($this->model);
        if($manage === false){
            return $this->error($manage->getError());
        }

        $html = '';
        $info = $manage->onShow();
        $form = $this->adminForm();

        $model   = new Order();
        $refunds = [1 => '已经申请', 0 => '未申请'];
        $sexes   = [1 => '男', 2 => '女', 0 => '保密'];

        // 活动
        $eInfo = (new Event())->isCache(true)->getOneById($info['event_id'] ?? 0);
        $uInfo = (new User())->isCache(true)->getOneById($info['uid'] ?? 0);

        // 用户名
        //$status      = $form->formRadio('status', $info['status'] ?? 1, $statuss);
        //$uid      = $form->ajaxSearch('uid', $info['uid'] ?? '', 'User');
        //$uid      = $form->ajaxSingleFromList('uid', $uInfo, 'User', 'id', 'username');
        $uid        = $form->ajaxSingleFromList('uid', $uInfo, 'User', 'id', 'username');
        $order_uid  = $form->ajaxSingleFromList('order_uid', $uInfo, 'User', 'id', 'username');
        $event_id   = $form->ajaxSingleFromList('event_id', $eInfo, 'Event', 'id', 'event_name');
        $student_id = $form->ajaxSingleFromList('student_id', '学员ID', 'Student', 'id', 'person_name');


        $order_no = $form->formInput('order_no', $info['order_no'] ?? '', '订单号');


        $head_img    = $form->formImage('head_img', $info['head_img'] ?? '');
        $sex         = $form->formRadio('sex', $info['sex'] ?? 1, $sexes);
        $age         = $form->formNumber('age', $info['age'] ?? '', '年龄');
        $old_id      = $form->formNumber('old_id', $info['old_id'] ?? '', '退款的订单ID');
        $person_name = $form->formInput('person_name', $info['person_name'] ?? '', '姓名');
        $guardor     = $form->formInput('guardor', $info['guardor'] ?? '', '监护人');
        $mobile      = $form->formInput('mobile', $info['mobile'] ?? '', '手机号');
        $is_refund   = $form->formRadio('is_refund', $info['is_refund'] ?? 1, $refunds);
        $_id         = $form->formHidden('id', $info['id'] ?? '');

        //$html     .= $form->makeItem('确认密码', $password, '添加时候不填默认密码123456，修改不填默认不修改');
        $html .= $form->makeItem('用户', $uid);
        $html .= $form->makeItem('活动', $event_id);
        $html .= $form->makeItem('订单号', $order_id);
        $html .= $form->makeItem('学员', $student_id);
        $html .= $form->makeItem('订单用户', $order_uid);
        $html .= $form->makeItem('姓名', $person_name);
        $html .= $form->makeItem('头像', $head_img);
        $html .= $form->makeItem('性别', $sex);
        $html .= $form->makeItem('年龄', $age);
        $html .= $form->makeItem('监护人', $guardor);
        $html .= $form->makeItem('手机号', $mobile);
        $html .= $form->makeItem('退款的订单ID', $old_id);
        $html .= $form->makeItem('是否退款', $is_refund);
        //$html .= $form->makeItem('状态', $status);
        //$html .= $form->makeItem('退款状态', $status_refund);
        $html .= $_id;

        return $html;
    }

}


